Chapter 6

Code example 6-1
Private Sub Set_Color_Click()
'Change the color of the label named Label0.
Dim intColor As Integer
intColor = InputBox("Enter a number between 1 and 4, _
"Set Color")
Label0.BackStyle = Normal    'Change from Transparent
'Then set the desired color
Select Case intColor
Case 1            'Sets to red        
Label0.Color.BackColor = 255
Case 2            'Sets to green                    Label0.Color.BackColor = 845388
Case 3            'Sets to blue            
Label0.Color.BackColor = 16711680
Case 4            'Returns color to gray                    Label0.Color.BackColor = 12632256
Case Else        'Number entered not 1, 2, 3, or 4            
MsgBox ("Invalid number. Please enter 1-4")
End Select
End Sub
Code example 6-2
Function LabelAddress() As String
'Uses ASCII characters to cause a carriage return and line feed.
Dim strLineFeed As String
strLineFeed = Chr(13) & Chr(10)    
LabelAddress = FullName & strLineFeed & _
Address & strLineFeed & ", " & _
State & " " & ZipCode
End Function
Code example 6-3
Sub LogicComp()
Dim curPrice As Currency, curCost As Currency
Dim blnEcon101 As Boolean
curPrice = 150
curCost = 35
'Both expressions are True.
blnEcon101 = curPrice > 100 AND curCost < 50        'Returns True
'First expression is True, the second is False.
blnEcon101 = curPrice > 100 OR curCost > 50        'Returns True
'Both expressions are False.
blnEcon101 = curPrice < 100 XOR curCost < 50        'Returns True
End Sub
Code example 6-4
Sub ChangeLabel()
Me.Label0.Height = 100
Me.Label0.Width = 200
Me.Label0.Caption = "My Label"
Me.Label0.Font.Color = 255
Me.Label0.Font.Bold = True
Me.Label0.Font.Italic = True
Me.Label0.Fint.Size = 16
End Sub
Code example 6-5
Private Sub Form_Open(Cancel As Integer)
Dim btn As Object
For Each btn In Me.Controls
    If btn.ControlType = 104 Then    'It is a command button
        btn.ForeColor = 255
btn.FontBold = True
If btn.Name = "Cancel" Then
            Exit For
             End If
        btn.Caption = "My Button"
    End If
Next
End Sub
Code example 6-6
Sub ForEach()
Dim varArray(10), Item
Dim i As Integer
For i = 1 To 10
    varArray(i) = 2.5 * i
Next i
For Each Item In varArray
    Item = Item * (-1)    
    Debug.Print Item
Next Item
End Sub
Code example 6-7
Function GetDiscount(Sale As Single) As Single
GetDiscount = 0       'Sets discount to default 0
If Sale>1000 Then
    GetDiscount = .85 'Returns an 85% discount
End If
End Function
Code example 6-8
Private Sub Preview_Click()
   Select Case [Preview Reports]
       Case 1
DoCmd.OpenReport "emails", acViewPreview, "", "", _                          acNormal
       Case 2
        DoCmd.OpenReport "CdHrs", acViewPreview, "", "", _ 
        acNormal                                                                                                                                                                                                                
       Case 3
                DoCmd.OpenReport "HiLites", acViewPreview, "", "", _    
acNormal
End Select
End Sub
Access Power Programming with VBA, 8/23/2003, Web code examples
Virginia Andersen

